package com.lkz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkz.entity.DeptRole;
import com.lkz.entity.Role;
import com.lkz.entity.RolePermission;
import com.lkz.mapper.DeptRoleMapper;
import com.lkz.mapper.RoleMapper;
import com.lkz.mapper.RolePermissionMapper;
import com.lkz.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkz.utils.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper ;
    @Resource
    private DeptRoleMapper deptRoleMapper ;
    @Override
    public IPage<Role> selectList(Page<Role> page) {
        return roleMapper.selectList(page);
    }

    @Override
    public IPage<Role> selectByDel(Page<Role> page) {
        return roleMapper.selectByDel(page);
    }

    @Override
    public Integer add(Role role, Integer deptId, Integer permId) {
        role.setCreateTime(StringUtils.getNowTime());
        role.setUpdateTime(role.getCreateTime());
        role.setIsDel(0);
        int result = roleMapper.insert(role);

        DeptRole deptRole = new DeptRole(deptId,role.getRoleId());
        deptRole.setCreateTime(StringUtils.getNowTime());
        deptRole.setUpdateTime(deptRole.getCreateTime());
        deptRole.setIsDel(0);
        deptRoleMapper.insert(deptRole);

        RolePermission permission = new RolePermission(role.getRoleId(),permId);
        permission.setCreateTime(StringUtils.getNowTime());
        permission.setUpdateTime(deptRole.getCreateTime());
        permission.setIsDel(0);
        rolePermissionMapper.insert(permission);

        return result;
    }

    @Override
    public Integer update(Role role, Integer deptId, Integer permId) {
        role.setUpdateTime(StringUtils.getNowTime());
        int result =  roleMapper.updateById(role);

        DeptRole deptRole = new DeptRole(deptId,role.getRoleId());
        deptRole.setUpdateTime(role.getUpdateTime());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dept_id",deptId);
        Integer update = deptRoleMapper.update(deptRole,queryWrapper);
        if(update!=1){
            deptRole.setCreateTime(StringUtils.getNowTime());
            deptRole.setUpdateTime(deptRole.getCreateTime());
            deptRole.setIsDel(0);
            deptRoleMapper.insert(deptRole);
        }

        RolePermission permission = new RolePermission(role.getRoleId(),permId);
        permission.setUpdateTime(StringUtils.getNowTime());
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper.eq("perm_id",permId);
        Integer update2 = rolePermissionMapper.update(permission,queryWrapper);
        if(update2!=1){
            permission.setCreateTime(StringUtils.getNowTime());
            permission.setUpdateTime(deptRole.getCreateTime());
            permission.setIsDel(0);
            rolePermissionMapper.insert(permission);
        }
        return result;
    }
    @Override
    public Map<String,Object> uploadExcl(MultipartFile file) {
        Map<String,Object> ruslt = new HashMap<>();
        try {
            String fileName = file.getOriginalFilename();
            Workbook workbook;
            if(fileName.endsWith("xls")){
                workbook = new HSSFWorkbook(file.getInputStream());
            }else if(fileName.endsWith("xlsx")){
                workbook = new XSSFWorkbook(file.getInputStream());
            } else {
                ruslt.put("code","1");
                ruslt.put("message","文件格式非excl");
                return ruslt;
            }
            //判断第一页不为空
            if(null != workbook.getSheetAt(0)){
                //读取excl第二行，从1开始
                for(int rowNumofSheet = 1;rowNumofSheet <=workbook.getSheetAt(0).getLastRowNum();rowNumofSheet++){
                    if (null != workbook.getSheetAt(0).getRow(rowNumofSheet)) {
                        //定义行，并赋值
                        Row aRow = workbook.getSheetAt(0).getRow(rowNumofSheet);
                        Role role = new Role();
                        System.out.println(aRow.getLastCellNum());
                        for(int cellNumofRow=0;cellNumofRow<aRow.getLastCellNum();cellNumofRow++){
                            //读取rowNumOfSheet值所对应行的数据
                            //获得行的列数
                            Cell xCell = aRow.getCell(cellNumofRow);
                            Object cell_val;
                            if(cellNumofRow == 0){
                                if(xCell != null && !xCell.toString().trim().isEmpty()){
                                    cell_val = xCell.getStringCellValue();
                                    if(cell_val != null){
                                        String temp = (String)cell_val;
                                        role.setRoleName(temp);
                                        System.out.println("------------------------->"+temp);
                                        Date date = new Date();
                                        role.setCreateTime(StringUtils.getNowTime());
                                        role.setUpdateTime(role.getCreateTime());
                                        role.setIsDel(0);
                                        roleMapper.insert(role);
                                    }
                                }
                            }
                        }

                    }
                }
                ruslt.put("code","0");
                ruslt.put("message","成功插入数据库！");
            }else {
                ruslt.put("code","1");
                ruslt.put("message","第一页EXCL无数据！");
            }
        }catch (Exception e){
            e.printStackTrace();
            ruslt.put("code","1");
            ruslt.put("message",e.getMessage());
        }
        return ruslt;
    }

}
